package com.project.controller.thymelef;

import com.github.pagehelper.PageInfo;
import com.project.bean.StudentBean;
import com.project.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("sc")
@RequestMapping("thy")
public class StudentController {
    @Autowired
    private IStudentService service;

    @RequestMapping("/findAll")
    public String findAll(
            @RequestParam(value="pageNO",defaultValue = "1") int pageNO,
            Model model){
        //调用业务方法，得到分页对象
        PageInfo<StudentBean> pageInfo = service.findAll(pageNO);
        //在请求范围内，绑定共享数据
        model.addAttribute("pageObj", pageInfo);

        return "/html/student/index.html";
    }

    @RequestMapping("/add")
    public String add(StudentBean student,Model model){
        service.add(student);

        return "redirect:/thy/findAll";
    }

    @RequestMapping("/del")
    public String del(int id){
        service.del(id);
        return "redirect:/thy/findAll";
    }

    @RequestMapping("/findById")
    public String findById(int id,Model model){
        StudentBean student = service.findById(id);
        System.out.println(student);
        model.addAttribute("student", student);

        return "/html/student/update.html";
    }

    @RequestMapping("update")
    public String update(int id,int grade){
        service.update(id, grade);

        return "redirect:/thy/findAll";
    }

    @RequestMapping("getStr")
    @ResponseBody
    public  String getStr(){
        return "ok";
    }
}
